﻿<UserControl x:Name="_root"
	x:Class="Shortcuts.CommandsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:con="http://schemas.kent.boogaart.com/converters"
	xmlns:sys="clr-namespace:System;assembly=mscorlib"
	xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
	<DockPanel DataContext="{Binding ElementName=_root}">
		<DockPanel.Resources>
			<CollectionViewSource x:Key="GroupedCommands" Source="{Binding CommandService.RegisteredCommands}">
				<CollectionViewSource.GroupDescriptions>
					<PropertyGroupDescription PropertyName="Category"/>
				</CollectionViewSource.GroupDescriptions>
				<CollectionViewSource.SortDescriptions>
					<scm:SortDescription PropertyName="Category"/>
					<scm:SortDescription PropertyName="Text"/>
				</CollectionViewSource.SortDescriptions>
			</CollectionViewSource>

			<con:MapConverter x:Key="OkButtonEnabledConverter">
				<con:Mapping>
					<con:Mapping.From>
						<sys:Int32>0</sys:Int32>
					</con:Mapping.From>
					<con:Mapping.To>
						<sys:Boolean>False</sys:Boolean>
					</con:Mapping.To>
				</con:Mapping>
			</con:MapConverter>
		</DockPanel.Resources>
		
		<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
			<Button IsDefault="True" IsEnabled="{Binding SelectedItems.Count, ElementName=_commandsListBox, Converter={StaticResource OkButtonEnabledConverter}, FallbackValue=True}" Click="_okButton_Click">_OK</Button>
			<Button IsCancel="True">_Cancel</Button>
		</StackPanel>
		<ListBox x:Name="_commandsListBox" ItemsSource="{Binding Source={StaticResource GroupedCommands}}" SelectionMode="Extended" Grid.IsSharedSizeScope="True">
			<ListBox.GroupStyle>
				<GroupStyle>
					<GroupStyle.HeaderTemplate>
						<DataTemplate>
							<ContentControl>
								<Bold><TextBlock Text="{Binding Name, Converter={con:FormatConverter {}{0} Commands:}}"/></Bold>
							</ContentControl>
						</DataTemplate>
					</GroupStyle.HeaderTemplate>
				</GroupStyle>
			</ListBox.GroupStyle>
			<ListBox.ItemTemplate>
				<DataTemplate>
					<Grid>
						<Grid.RowDefinitions>
							<RowDefinition Height="Auto" SharedSizeGroup="CommandRow"/>
						</Grid.RowDefinitions>
						<Grid.ColumnDefinitions>
							<ColumnDefinition Width="Auto" SharedSizeGroup="CommandImage"/>
							<ColumnDefinition Width="*" SharedSizeGroup="CommandName"/>
						</Grid.ColumnDefinitions>
						
						<Image Grid.Column="0" Source="{Binding Image}" MaxWidth="25" Margin="3"/>
						<TextBlock Grid.Column="1" Text="{Binding Text}" VerticalAlignment="Center"/>
					</Grid>
				</DataTemplate>
			</ListBox.ItemTemplate>
		</ListBox>
	</DockPanel>
</UserControl>
